qx.Class.define("elpintao.frms.pedidos.pagePedidosExt",
{
	extend : qx.ui.tabview.Page,
	construct : function (appMain)
	{
	this.base(arguments);
	
	this.setLabel('Pedidos externos');
	this.setLayout(new qx.ui.layout.Canvas());
	this.toggleShowCloseButton();
	
	var contexto = this;
	var id_fabrica = "0";
	
	var functionCalcularTotales = function(tableModelD, tableModelT) {
		var rowDataAsMapDetalle, rowDataDetalle;
		var rowDataAsMapTotales, rowDataTotales;
		var bandera;
		
		tableModelT.setDataAsMapArray([{descrip: "P.lis.", total: 0}, {descrip: "P.lis.+IVA", total: 0}], true);
		
		for (var i = 0; i < tableModelD.getRowCount(); i++) {
			rowDataAsMapDetalle = tableModelD.getRowDataAsMap(i);
			rowDataDetalle = tableModelD.getRowData(i);
			
			rowDataAsMapTotales = tableModelT.getRowDataAsMap(0);
			tableModelT.setValueById("total", 0, rowDataAsMapTotales.total + rowDataAsMapDetalle.precio_lista);
			rowDataAsMapTotales = tableModelT.getRowDataAsMap(1);
			tableModelT.setValueById("total", 1, rowDataAsMapTotales.total + rowDataAsMapDetalle.plmasiva);
			bandera = true;
			for (var x = 2; x < tableModelT.getRowCount(); x++) {
				rowDataAsMapTotales = tableModelT.getRowDataAsMap(x);
				rowDataTotales = tableModelT.getRowData(x);
				if (rowDataDetalle.id_unidad == rowDataTotales.id_unidad) {
					tableModelT.setValueById("total", x, tableModelT.getValueById("total", x) + rowDataAsMapDetalle.cantidad)
					bandera = false;
					break;
				}
			}
			if (bandera) {
				tableModelT.addRowsAsMapArray([{id_unidad: rowDataDetalle.id_unidad, descrip: rowDataAsMapDetalle.unidad, total: rowDataAsMapDetalle.cantidad}], null, true);
			}
		}
	}
	
	
	var stack = new qx.ui.container.Stack();
	var composite1 = new qx.ui.container.Composite(new qx.ui.layout.Canvas());
	var composite2 = new qx.ui.container.Composite(new qx.ui.layout.Canvas());
	stack.add(composite1);
	stack.add(composite2);


		
	
	//Tabla

	var tableModelPedidoInt = new qx.ui.table.model.Simple();
	tableModelPedidoInt.setColumns(["", "Sucursal", "Fecha", "Fábrica", "Producto", "Color", "Capacidad", "U", "Cantidad"], ["seleccionado", "sucursal", "fecha", "fabrica", "producto", "color", "capacidad", "unidad", "cantidad"]);
	//tableModelPedido.setColumns(["Fecha", "Fábrica"], ["fecha", "id_fabrica"]);
	//tableModelPedido.setEditable(true);

	var custom = {tableColumnModel : function(obj) {
		return new qx.ui.table.columnmodel.Resize(obj);
	}};
	
	var tblPedidoInt = new controles.table.Table(tableModelPedidoInt, custom);
	tblPedidoInt.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
	//tblPedido.setShowCellFocusIndicator(true);
	tblPedidoInt.toggleColumnVisibilityButtonVisible();
	tblPedidoInt.toggleStatusBarVisible();
	
	
	var tableColumnModelPedidoInt = tblPedidoInt.getTableColumnModel();
	//tableColumnModelPedido.setColumnWidth(0, 65);
	//tableColumnModelPedido.setColumnWidth(1, 65);
	
		var resizeBehavior = tableColumnModelPedidoInt.getBehavior();
		resizeBehavior.set(0, {width:"3%", minWidth:100});
		resizeBehavior.set(1, {width:"10%", minWidth:100});
		resizeBehavior.set(2, {width:"12%", minWidth:100});
		resizeBehavior.set(3, {width:"10%", minWidth:100});
		resizeBehavior.set(4, {width:"30%", minWidth:100});
		resizeBehavior.set(5, {width:"18%", minWidth:100});
		resizeBehavior.set(6, {width:"6%", minWidth:100});
		resizeBehavior.set(7, {width:"2.5%", minWidth:100});
		resizeBehavior.set(8, {width:"6%", minWidth:100});
	
	
	
	var cellrendererBoolean = new qx.ui.table.cellrenderer.Boolean();
	tableColumnModelPedidoInt.setDataCellRenderer(0, cellrendererBoolean);

	var selectionModelPedidoInt = tblPedidoInt.getSelectionModel();

	selectionModelPedidoInt.addListener("changeSelection", function(e){

	});
	
	tblPedidoInt.addListener("cellDblclick", function(e){
		var focusedRowPedido = tblPedidoInt.getFocusedRow();
		var rowDataPedido = tableModelPedidoInt.getRowData(focusedRowPedido);
		if (id_fabrica=="0" || id_fabrica == rowDataPedido.id_fabrica) {
			id_fabrica = rowDataPedido.id_fabrica;
			lblFabrica.setValue(rowDataPedido.fabrica);
			btnGenerar.setEnabled(true);
			btnBorrar.setEnabled(true);
			
			rowDataDetalle = tblDetalleInt.buscar("id_producto_item", rowDataPedido.id_producto_item);
			if (rowDataDetalle == null) {
				var aux = [qx.util.Json.parse(qx.util.Json.stringify(rowDataPedido))];
				tableModelDetalleInt.addRowsAsMapArray(aux, null, true);
				tblDetalleInt.setFocusedCell(0, tableModelDetalleInt.getRowCount() - 1, true);
			} else {
				if (rowDataPedido.seleccionado) {
					rowDataDetalle.cantidad = rowDataDetalle.cantidad - rowDataPedido.cantidad
				} else {
					rowDataDetalle.cantidad = rowDataDetalle.cantidad + rowDataPedido.cantidad
				}
				var focusedRowDetalle = tblDetalleInt.getFocusedRow();
				if (rowDataDetalle.cantidad == 0) {
					tblDetalleInt.setFocusedCell();
					tableModelDetalleInt.removeRows(focusedRowDetalle, 1);
				} else {
					tableModelDetalleInt.setRowsAsMapArray([rowDataDetalle], focusedRowDetalle, true);
				}
			}
			rowDataPedido.seleccionado = ! rowDataPedido.seleccionado;
			tableModelPedidoInt.setRowsAsMapArray([rowDataPedido], focusedRowPedido, true);
			
			if (tableModelDetalleInt.getRowCount()==0) {
				id_fabrica = "0";
				lblFabrica.setValue("");
				btnGenerar.setEnabled(false);
				btnBorrar.setEnabled(false);
			}
			functionCalcularTotales(tableModelDetalleInt, tableModelTotalesInt);
		}
	});

	composite1.add(tblPedidoInt, {left:0 , top: 0, right: 0, bottom: "56%"});
	//composite1.add(new qx.ui.basic.Label("Pedidos:"), {left:0 , top: 0});
	
	

	composite1.add(new qx.ui.basic.Label("Fábrica:"), {left:0 , top: "45%"});
	var lblFabrica = new qx.ui.basic.Label("");
	composite1.add(lblFabrica, {left:0 , top: "49%"});

	composite1.add(new qx.ui.basic.Label("Teléfono:"), {left:150 , top: "45%"});
	var txtTelefono = new qx.ui.form.TextField();
	txtTelefono.setWidth(140);
	composite1.add(txtTelefono, {left:150 , top: "49%"});
	
	composite1.add(new qx.ui.basic.Label("E-mail:"), {left:300 , top: "45%"});
	var txtEmail = new qx.ui.form.TextField();
	txtEmail.setWidth(140);
	composite1.add(txtEmail, {left:300 , top: "49%"});
	
	composite1.add(new qx.ui.basic.Label("Transporte:"), {left:450 , top: "45%"});

	composite1.add(new qx.ui.basic.Label("Dom.Entrega:"), {left:600 , top: "45%"});
	var txtDomEntrega = new qx.ui.form.TextField();
	txtDomEntrega.setWidth(140);
	composite1.add(txtDomEntrega, {left:600 , top: "49%"});
	
	var btnGenerar = new qx.ui.form.Button("Generar pedido");
	btnGenerar.setEnabled(false);
	btnGenerar.addListener("execute", function(e){
		var rowDataDetalleInt;
		var p = {};
		p.model = {id_fabrica: id_fabrica, telefono: txtTelefono.getValue(), email: txtEmail.getValue(), id_transporte: 1, domicilio: txtDomEntrega.getValue()};
		p.detalle = [];
		for (var i = 0; i < tableModelDetalleInt.getRowCount(); i++) {
			rowDataDetalleInt = tableModelDetalleInt.getRowData(i);
			p.detalle.push({id_producto_item: rowDataDetalleInt.id_producto_item, cantidad: rowDataDetalleInt.cantidad});
		}

		var rpc = new qx.io.remote.Rpc("services/", "elpintao.PedidosExt");
		try {
			var resultado = rpc.callSync("alta_pedido_ext", p);
		} catch (ex) {
			alert("Sync exception: " + ex);
		}

		tableModelPedido.setDataAsMapArray(resultado, true, true);
		tblPedido.setFocusedCell(0, tableModelPedido.getRowCount() - 1, true);
		rbVacunacion.fireEvent("execute");
		tblPedido.focus();
		
		btnBorrar.fireEvent("execute");
	});
	composite1.add(btnGenerar, {left:800 , top: "49%"});
	
	var btnBorrar = new qx.ui.form.Button("Borrar");
	btnBorrar.setEnabled(false);
	btnBorrar.addListener("execute", function(e){
		tableModelDetalleInt.setDataAsMapArray([], true);
		for (var i = 0; i < tableModelPedidoInt.getRowCount(); i++) {
			tableModelPedidoInt.getRowData(i).seleccionado = false;
			tableModelPedidoInt.setValueById("seleccionado", i, false);
		}
		txtTelefono.setValue("");
		txtEmail.setValue("");
		txtDomEntrega.setValue("");
		id_fabrica = "0";
		lblFabrica.setValue("");
		btnGenerar.setEnabled(false);
		btnBorrar.setEnabled(false);
		functionCalcularTotales(tableModelDetalleInt, tableModelTotalesInt);
	});
	composite1.add(btnBorrar, {left:900 , top: "49%"});
	
	
	
	
	
	//Tabla

	var tableModelDetalleInt = new qx.ui.table.model.Simple();
	tableModelDetalleInt.setColumns(["Producto", "Color", "Capacidad", "U", "P.lis.", "P.lis.+IVA", "Cantidad"], ["producto", "color", "capacidad", "unidad", "precio_lista", "plmasiva", "cantidad"]);
	
	//tableModelDetalle.setEditable(true);
	//tableModelDetalle.setColumnEditable(4, true);

	var custom = {tableColumnModel : function(obj) {
		return new qx.ui.table.columnmodel.Resize(obj);
	}};
	
	var tblDetalleInt = new controles.table.Table(tableModelDetalleInt, custom);
	tblDetalleInt.sentidoDeEdicion = "vertical";
	tblDetalleInt.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
	//tblDetalle.toggleColumnVisibilityButtonVisible();
	//tblDetalle.setShowCellFocusIndicator(true);
	tblDetalleInt.toggleColumnVisibilityButtonVisible();
	tblDetalleInt.toggleStatusBarVisible();
	
	var tableColumnModelDetalleInt = tblDetalleInt.getTableColumnModel();
	//tableColumnModelDetalle.setColumnVisible(7, false);
	
      // Obtain the behavior object to manipulate

		var resizeBehavior = tableColumnModelDetalleInt.getBehavior();
		resizeBehavior.set(0, {width:"47%", minWidth:100});
		resizeBehavior.set(1, {width:"18%", minWidth:100});
		resizeBehavior.set(2, {width:"7%", minWidth:100});
		resizeBehavior.set(3, {width:"3%", minWidth:100});
		resizeBehavior.set(4, {width:"7%", minWidth:100});
		resizeBehavior.set(5, {width:"8%", minWidth:100});
		resizeBehavior.set(6, {width:"7%", minWidth:100});


	var selectionModelDetalleInt = tblDetalleInt.getSelectionModel();
	selectionModelDetalleInt.addListener("changeSelection", function(e){

	});
	
	composite1.add(tblDetalleInt, {left:0, top: "55%", right: "15.5%", bottom: 0});
	//composite1.add(new qx.ui.basic.Label("Detalle:"), {left: 0, top: "47%"});

	
	
	//Tabla

	var tableModelTotalesInt = new qx.ui.table.model.Simple();
	tableModelTotalesInt.setColumns(["", "Total"], ["descrip", "total"]);

	var custom = {tableColumnModel : function(obj) {
		return new qx.ui.table.columnmodel.Resize(obj);
	}};
	
	var tblTotalesInt = new controles.table.Table(tableModelTotalesInt, custom);
	//tblTotales.toggleShowCellFocusIndicator();
	tblTotalesInt.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
	//tblTotales.setShowCellFocusIndicator(false);
	tblTotalesInt.toggleColumnVisibilityButtonVisible();
	tblTotalesInt.toggleStatusBarVisible();
	
	composite1.add(tblTotalesInt, {left: "85%", top: "55%", right: 0, bottom: 0});
	

	functionCalcularTotales(tableModelDetalleInt, tableModelTotalesInt);
	
	
	
		var toolbar = new qx.ui.toolbar.ToolBar();
		var rbVisitas = new qx.ui.toolbar.RadioButton(" Pedidos internos de sucursales ");
		var rbVacunacion = new qx.ui.toolbar.RadioButton(" Pedidos externos actuales ");

		rbVisitas.addListener("execute", function(){stack.setSelection([composite1]);});
		rbVacunacion.addListener("execute", function(){stack.setSelection([composite2]);});
		
		toolbar.add(rbVisitas);
		//toolbar.addSpacer();
		toolbar.add(rbVacunacion);
		var radioGroup = new qx.ui.form.RadioGroup(rbVisitas, rbVacunacion);

		this.add(toolbar, {left: 0, top: 0});
		this.add(stack, {left: 0, top: 31, right: 0, bottom: 0});
		
		

	

	//Tabla

	var tableModelPedido = new qx.ui.table.model.Simple();
	tableModelPedido.setColumns(["Fecha", "Fábrica", "Teléfono", "E-mail", "Transporte", "Domic.entrega"], ["fecha", "fabrica", "telefono", "email", "transporte", "domicilio"]);
	//tableModelPedido.setColumns(["Fecha", "Fábrica"], ["fecha", "id_fabrica"]);
	//tableModelPedido.setEditable(true);
	

	var custom = {tableColumnModel : function(obj) {
		return new qx.ui.table.columnmodel.Resize(obj);
	}};
	
	var tblPedido = new controles.table.Table(tableModelPedido, custom);
	tblPedido.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
	//tblPedido.setShowCellFocusIndicator(true);
	tblPedido.toggleColumnVisibilityButtonVisible();
	tblPedido.toggleStatusBarVisible();
	
	
	var tableColumnModelPedido = tblPedido.getTableColumnModel();
	//tableColumnModelPedido.setColumnWidth(0, 65);
	//tableColumnModelPedido.setColumnWidth(1, 65);



	var selectionModelPedido = tblPedido.getSelectionModel();

	selectionModelPedido.addListener("changeSelection", function(e){
		if (selectionModelPedido.getSelectedCount() > 0) {
			var rowData = tableModelPedido.getRowData(tblPedido.getFocusedRow());
			tableModelDetalle.setDataAsMapArray(rowData.detalle, true);
			functionCalcularTotales(tableModelDetalle, tableModelTotales);
		}
	});

	composite2.add(tblPedido, {left:0 , top: 0, right: 0, bottom: "54%"});
	
	
	//Tabla
	
	var tableModelDetalle = new qx.ui.table.model.Simple();
	tableModelDetalle.setColumns(["Producto", "Color", "Capacidad", "U", "P.lis.", "P.lis.+IVA", "Cantidad"], ["producto", "color", "capacidad", "unidad", "precio_lista", "plmasiva", "cantidad"]);
	
	//tableModelDetalle.setEditable(true);
	//tableModelDetalle.setColumnEditable(4, true);

	var custom = {tableColumnModel : function(obj) {
		return new qx.ui.table.columnmodel.Resize(obj);
	}};
	
	var tblDetalle = new controles.table.Table(tableModelDetalle, custom);
	tblDetalle.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
	//tblDetalle.toggleColumnVisibilityButtonVisible();
	tblDetalle.toggleColumnVisibilityButtonVisible();
	tblDetalle.toggleStatusBarVisible();
	
	var tableColumnModelDetalle = tblDetalle.getTableColumnModel();
	//tableColumnModelDetalle.setColumnVisible(7, false);
	

      // Obtain the behavior object to manipulate

		var resizeBehavior = tableColumnModelDetalle.getBehavior();
		resizeBehavior.set(0, {width:"47%", minWidth:100});
		resizeBehavior.set(1, {width:"18%", minWidth:100});
		resizeBehavior.set(2, {width:"7%", minWidth:100});
		resizeBehavior.set(3, {width:"3%", minWidth:100});
		resizeBehavior.set(4, {width:"7%", minWidth:100});
		resizeBehavior.set(5, {width:"8%", minWidth:100});
		resizeBehavior.set(6, {width:"7%", minWidth:100});

		
	
	var selectionModelDetalle = tblDetalle.getSelectionModel();
	selectionModelDetalle.addListener("changeSelection", function(e){

	});
	
	
	composite2.add(tblDetalle, {left:0, top: "50%", right: "15.5%", bottom: 0});
	
	//this.add(tblPedido, {left:0 , top: 20, right: 0, height: "40%"});
	
	composite2.add(new qx.ui.basic.Label("Detalle:"), {left: 0, top: "47%"});
	
	
	
	//Tabla

	var tableModelTotales = new qx.ui.table.model.Simple();
	tableModelTotales.setColumns(["", "Total"], ["descrip", "total"]);

	var custom = {tableColumnModel : function(obj) {
		return new qx.ui.table.columnmodel.Resize(obj);
	}};
	
	var tblTotales = new controles.table.Table(tableModelTotales, custom);
	//tblTotales.toggleShowCellFocusIndicator();
	tblTotales.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
	//tblTotales.setShowCellFocusIndicator(false);
	tblTotales.toggleColumnVisibilityButtonVisible();
	tblTotales.toggleStatusBarVisible();
	
	composite2.add(tblTotales, {left: "85%", top: "50%", right: 0, bottom: 0});	
	
	functionCalcularTotales(tableModelDetalle, tableModelTotales);

	
	var rpc = new qx.io.remote.Rpc("services/", "elpintao.PedidosExt");
	try {
		var resultado = rpc.callSync("leer_pedido");
	} catch (ex) {
		alert("Sync exception: " + ex);
	}
	//alert(qx.util.Json.stringify(resultado, true));
	tableModelPedidoInt.setDataAsMapArray(resultado.internos, true, true)
	if (tableModelPedidoInt.getRowCount() > 0) tblPedidoInt.setFocusedCell(1, 0, true);
	
	tableModelPedido.setDataAsMapArray(resultado.externos, true, true)
	
	
	
	
	

	this.addListenerOnce("appear", function(e){
		tblPedidoInt.focus();
	});
	
	},
	members : 
	{

	}
});